package com.lisx.module_memo.dialogfragment;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.ijianji.lsx_ui_lib.BaseDialogFragment;
import com.lisx.module_memo.R;

public class AddMemoDialogFragment extends BaseDialogFragment {

    MemoDbEntity memoDbEntity;
    @Override
    public int getLayoutRes() {
        return R.layout.dialog_fragment_add_memo;
    }

    @Override
    public void initView(Context context, View view, AttributeSet attributeSet) {
        EditText etMsg = view.findViewById(R.id.et_msg);
        ImageView ivComplete = view.findViewById(R.id.iv_complete);
        RelativeLayout rlComplete = view.findViewById(R.id.rl_complete);
        etMsg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString().trim())) {
                    ivComplete.setImageResource(R.drawable.aa_btn_nor_wancheng);
                    rlComplete.setEnabled(false);
                } else {
                    ivComplete.setImageResource(R.drawable.aa_btn_act_wancheng);
                    rlComplete.setEnabled(true);
                }
            }
        });
        rlComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onAddMemo(etMsg.getText().toString().trim());
                }
                dismiss();
            }
        });
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        if (memoDbEntity != null && !TextUtils.isEmpty(memoDbEntity.getMsg())) {
            etMsg.setText(memoDbEntity.getMsg());
        }
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {

    }

    @Override
    public int getGravity() {
        return Gravity.BOTTOM;
    }

    public void setData(MemoDbEntity memoDbEntity) {
        this.memoDbEntity = memoDbEntity;
    }

    public interface OnAddMemoListener {
        void onAddMemo(String msg);
    }

    OnAddMemoListener listener;

    public void setOnAddMemoListener(OnAddMemoListener listener) {
        this.listener = listener;
    }

}
